﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;

namespace BookClubIt
{
    public partial class bookdetail : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label lblAmazonUrl = FindControl("lblAmazonUrl") as Label;

            int bid = 0;
            int.TryParse(Request.QueryString["id"], out bid);

            DTO.BookClubs clubs = BookClubProcessorInstance.GetBookClubsByBookReading(bid);
            if (clubs.Count > 0)
            {
                grdClubsReading.DataSource = clubs;
                grdClubsReading.DataBind();
            }
            else
            {
                pnlNoResults.Visible = true;
            }
            DTO.Book book = BookProcessorInstance.GetBookById(bid);
            Page.Title = book.Title;
            Page.MetaDescription = book.Editorial;


            lblTitle.Text = book.Title;
            lblAuthor.Text = book.Author;
            lblBinding.Text = book.Binding;
            lblISBN.Text = book.ISBN;
            lblPages.Text = book.Pages;
            lblPublishedDate.Text = book.PublishedDate.ToString();
            lblPublisher.Text = book.Publisher;
            lblEditorial.Text = book.Editorial;
            imgBookCover.ImageUrl = book.MediumImageURL;
            lnkAmazonUrl.NavigateUrl = book.AmazonURL;
        }
        public void BindData(DTO.BookClubs clubs)
        {
            grdClubsReading.DataSource = clubs;
            grdClubsReading.DataBind();
        }

        protected void grdClubsReading_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {

            DTO.BookClub club = e.Row.DataItem as DTO.BookClub;

            if (e.Row.RowType != DataControlRowType.DataRow)
                return;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Image imgClubAvatar = ((Image)e.Row.FindControl("imgClubAvatar"));
                Image imgCurrentBook = ((Image)e.Row.FindControl("imgCurrentBook"));
                HyperLink lnkBookClub = ((HyperLink)e.Row.FindControl("lnkBookClub"));
                HyperLink lnkViewClub = ((HyperLink)e.Row.FindControl("lnkViewClub"));
                Label lblClubType = ((Label)e.Row.FindControl("lblClubType"));
                Label lblNumMembers = ((Label)e.Row.FindControl("lblNumMembers"));
                //     Label lblCurrentBook = ((Label)e.Row.FindControl("lblCurrentBook"));
                Label lblCreatedDate = ((Label)e.Row.FindControl("lblCreatedDate"));
                Label lblGenreType = ((Label)e.Row.FindControl("lblGenreType"));

                int clubTypeInt = club.ClubType;
                int genreTypeInt = club.GenreType;
                int clubId = club.Id;
                //    string currentASIN = club.CurrentBook;

                DTO.ClubType type = BookClubProcessorInstance.GetBookClubType(clubTypeInt);
                DTO.GenreType gtype = BookClubProcessorInstance.GetBookClubGenre(genreTypeInt);
                DTO.Members members = MemberProcessorInstance.GetBookClubMembersById(clubId);
                DTO.Book current_book = BookProcessorInstance.GetCurrentBook(clubId);
                //    DTO.Book book = BookProcessorInstance.GetBookByASIN();

                imgClubAvatar.ImageUrl = "avatars/club/" + club.AvatarURL;
                lnkBookClub.Text = club.Name;
                lnkBookClub.NavigateUrl = "bookclub/default.aspx?id=" + club.Id;
                lnkViewClub.NavigateUrl = "bookclub/default.aspx?id=" + club.Id;

                //   lblCurrentBook.Text = current_book.Title;
                imgCurrentBook.ImageUrl = current_book.SmallImageURL;
                lblCreatedDate.Text = club.CreatedDate.ToString("MMM dd, yyyy") + " (" + TimeAgo(club.CreatedDate) + ")";
                lblGenreType.Text = gtype.GenreName;
                if (type.ClubTypeName == "Public")
                    lblClubType.CssClass = "status public";

                if (type.ClubTypeName == "Private")
                    lblClubType.CssClass = "status private";

                lblClubType.Text = type.ClubTypeName;

                lblNumMembers.Text = members.Count.ToString();

            }

        }
        static string TimeAgo(DateTime time)
        {
            DateTime startDate = DateTime.Now;
            TimeSpan deltaMinutes = startDate.Subtract(time);
            string distance = string.Empty;
            if (deltaMinutes.TotalMinutes <= (8724 * 60))
            {
                distance = DistanceOfTimeInWords(deltaMinutes.TotalMinutes);
                if (deltaMinutes.Minutes < 0)
                {
                    return distance + " from  now";
                }
                else
                {
                    return distance + " ago";
                }
            }
            else
            {
                return "on " + time.ToString();
            }
        }
        static string DistanceOfTimeInWords(double minutes)
        {
            if (minutes < 1)
            {
                return "less than a minute";
            }
            else if (minutes < 50)
            {
                return Math.Round(minutes).ToString() + " minutes";
            }
            else if (minutes < 90)
            {
                return "about one hour";
            }
            else if (minutes < 1080)
            {
                return Math.Round(new decimal((minutes / 60))).ToString() + " hours";
            }
            else if (minutes < 1440)
            {
                return "one day";
            }
            else if (minutes < 2880)
            {
                return "about one day";
            }
            else
            {
                return Math.Round(new decimal((minutes / 1440))).ToString() + " days";
            }
        }
    }
}